
var id="";
function wxPay(money,openid,recordId,showLogId) {
    id=showLogId;
    wx.miniProgram.getEnv(function(res) {
        if(res.miniprogram){
            //小程序
            payXcx(money,openid,recordId);
        }else {
            //公众号
            PayGzh(money,openid,recordId);
        }

    });
}

/**
 * H5 微信公众号支付
 * */
function PayGzh(money,openid,recordId){
    var url="/weiXin/pay";
    var params={
        "openid":openid,
        "money":money,
        "recordId":recordId
    };
    $.ajax({
        type: "POST",
        url: url,
        data: params,
        success: function(result){
            if(1==result.returnInfo.state){
                var entity=result.returnInfo.result;
                var result_code=entity.result_code;
                //$.alert("result_code:"+entity.result_code,"提示");
                var return_code=entity.return_code;
                //$.alert("return_code:"+entity.return_code);
                if("SUCCESS"==return_code){
                    if("SUCCESS"==result_code){
                        //$.alert("appid:"+entity.appid);
                        weixin_pay(entity.appid,entity.nonce_str,"prepay_id="+entity.prepay_id,entity.sign,entity.timestamp);
                    }else{
                        $.alert("err_code:"+entity.err_code);
                    }
                }else{
                    $.alert("return_msg:"+entity.return_msg);
                }
            }else{
                $.alert(result.returnInfo.result.errorMsg);
            }
        },
        error: function(result){
            $.alert(result);
        }
    });
}
function weixin_pay(appid,nonceStr,prepay_id,sign,timeStamp){
    //$.alert("appid:"+appid,"提示");
    //$.alert("timeStamp:"+timeStamp,"提示");
    //$.alert("nonceStr:"+nonceStr,"提示");
    //$.alert("prepay_id:"+prepay_id,"提示");
    //$.alert("sign:"+sign,"提示");
    WeixinJSBridge.invoke('getBrandWCPayRequest', {
        "appId" :appid, //公众号名称，由商户传入
        "timeStamp" : timeStamp, //时间戳
        "nonceStr" : nonceStr, //随机串
        "package" : prepay_id,//扩展包
        "signType" : "MD5", //微信签名方式:MD5
        "paySign" : sign //微信签名
    }, function (res) {
        if(res.err_msg == "get_brand_wcpay_request:ok" ){
            //$.alert("支付成功");
            window.location.href='/phone/result/'+id;
        }else if(res.err_msg == "get_brand_wcpay_request:cancel" ){
            $.alert("取消支付");
        }else{
            $.alert("支付失败"+res.errMsg);
        }
    });
}
function getTimeStamp(){
    var timestamp=new Date().getTime();
    var timestampstring = timestamp.toString();//一定要转换字符串
    return timestampstring;
}


/**
 * 微信小程序支付
 */

function payXcx(money,openid,recordId) {
    var url="/weiXin/xcxPay";
    var params={
        "openid":openid,
        "money":money,
        "recordId":recordId
    };
    $.ajax({
        type: "POST",
        url: url,
        data: params,
        success: function(result){
            if(1==result.returnInfo.state){
                var entity=result.returnInfo.result;
                var result_code=entity.result_code;
                //$.alert("result_code:"+entity.result_code,"提示");
                var return_code=entity.return_code;
                //$.alert("return_code:"+entity.return_code);
                if("SUCCESS"==return_code){
                    if("SUCCESS"==result_code){
                        //$.alert("appid:"+entity.appid);
                        wx.miniProgram.redirectTo({url: '/pages/pay/pay?id='+id+'&appId='+entity.appid+'&nonceStr='+entity.nonce_str+'&package='+entity.prepay_id+'&signType=MD5&timeStamp='+entity.timestamp+'&paySign='+entity.sign+'&money='+money+'&orderId='+recordId})
                    }else{
                        $.alert("err_code:"+entity.err_code);
                    }
                }else{
                    $.alert("return_msg:"+entity.return_msg);
                }
            }else{
                $.alert(result.returnInfo.result.errorMsg);
            }
        },
        error: function(result){
            $.alert(result);
        }
    });
}


